# -*- coding:utf-8 -*-
import requests
import re
import datetime
import time
import json
from sqlalchemy import Column, String,Integer, create_engine  # 导入包
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import pymysql
pymysql.install_as_MySQLdb()

#获取原码

cookies = {
    'UniqueID': 'xfxbgZKQTNlC0laj1534566549004',
    'Sites': '_21',
    '_ga': 'GA1.3.1038745649.1534566546',
    '_gid': 'GA1.3.1604362988.1534566546',
    '21_vq': '15',
}

headers = {
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Referer': 'http://www.cwl.gov.cn/kjxx/ssq/kjgg/',
    'X-Requested-With': 'XMLHttpRequest',
    'Connection': 'keep-alive',
}
def add_zero(n):
    if len(str(n)) == 1:
        return '00' + str(n)
    elif len(str(n)) == 2:
        return '0' + str(n)
    elif len(str(n)) == 3:
        return str(n)
   
   
# 创建对象的基类:
Base = declarative_base()


# 定义一个类
class TEMP_SSQ(Base):  # 表的名字:
	__tablename__ = 'TMP_SSQ'  # 表的结构:
	code = Column(String(100), primary_key=True)
	no1 = Column(String(100))
	no2 = Column(String(100))
	no3 = Column(String(100))
	no4 = Column(String(200))
	no5 = Column(String(100))
	no6 = Column(String(100))
	no7 = Column(String(100))
	date =Column(String(100))
	content =Column(String(100))
	poolmoney =Column(String(100))

# 初始化数据库连接:
db_engine = create_engine('mysql+mysqldb://root:yuanchuq1@106.15.193.176:3306/51job?charset=utf8',encoding='gbk', echo=True)
s = datetime.datetime.now() + datetime.timedelta(-7)
b_week_time = s.strftime('%Y-%m-%d')
today = time.strftime('%Y-%m-%d', time.localtime(time.time()))

# 创建DBSession类型:
DBSession = sessionmaker(bind=db_engine)
session = DBSession()
for q in range(1, 161):  # 每年基本在154期左右
  for y in range(2019, 2020):  # 从2013年到2018年
 
   params = (
            ('name', 'ssq'),
            ('issueCount', ''),
            ('issueStart', str(y) + add_zero(q)),
            ('issueEnd', str(y) + add_zero(q)),
            ('dayStart', ''),
            ('dayEnd', ''),
            ('pageNo', '')
        )
   url = 'http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice'

   response = requests.get(url=url, headers=headers, params=params, cookies=cookies)
   result = json.loads(response.text)['result']  # 得到返回的json数据
   for s in result:
       jobinfo = TEMP_SSQ(code=s['code'],no1=s['red'].split(',')[0],
                             no2=s['red'].split(',')[1],no3=s['red'].split(',')[2],
                             no4=s['red'].split(',')[3],
                             no5=s['red'].split(',')[4],no6=s['red'].split(',')[5],
                             no7=s['blue'],date=s['date'],content=s['content'],poolmoney=s['poolmoney'])
       session.add(jobinfo)
       session.commit()
   

